/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAO;

import DTO.CauHoiDTO;
import Utilities.DBUtils;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author THANHLNSE60620
 */
public class CauHoiDAO {

    public static boolean insertCauHoi(CauHoiDTO ch) {
        Connection con = null;
        PreparedStatement stm = null;
        ResultSet rs = null;
        try {
            con = Utilities.DBUtils.makeConnection();

            String sql = "INSERT INTO dbo.CauHoi (CauHoi, CustomerCode, MaNhanVien, NoiDungTraLoi, NgayGui)"
                    + "VALUES (?, ?, ?, ?,?)";

            stm = con.prepareStatement(sql);
            stm.setString(1, ch.getCauHoi());
            stm.setInt(2, ch.getCustomerCode());
            stm.setInt(3, ch.getMaNhanVien());
            stm.setString(4, ch.getNoiDungTraLoi());
            stm.setString(5, ch.getNgayGui());

            int row = stm.executeUpdate();
            if (row > 0) {
                return true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (rs != null) {
                    rs.close();
                }
                if (con != null) {
                    con.close();
                }
                if (stm != null) {
                    stm.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }

        }
        return false;
    }

    public static CauHoiDTO[] searchCauHoi(String ngayGui) {
        Connection con = null;
        PreparedStatement stm = null;
        ResultSet rs = null;
        try {
            con = Utilities.DBUtils.makeConnection();
            String sql = "SELECT * FROM dbo.CauHoi WHERE NgayGui = ?";
            stm = con.prepareStatement(sql);
            stm.setString(1, ngayGui);
            List list = new ArrayList();
            rs = stm.executeQuery();
            while (rs.next()) {
                int MaCauHoi = rs.getInt("MaCauHoi");
                String CauHoi = rs.getString("CauHoi");
                int CustomerCode = rs.getInt("CustomerCode");
                int MaNhanVien = rs.getInt("MaNhanVien");
                String NoiDungTraLoi = rs.getString("NoiDungTraLoi");
                String NgayGui = rs.getString("NgayGui");
                CauHoiDTO ch = new CauHoiDTO(MaCauHoi, CauHoi, CustomerCode, MaNhanVien, NoiDungTraLoi,NgayGui);
                list.add(ch);
            }
            CauHoiDTO[] result = new CauHoiDTO[list.size()];
            list.toArray(result);
            return result;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (rs != null) {
                    rs.close();
                }
                if (con != null) {
                    con.close();
                }
                if (stm != null) {
                    stm.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }

        }
        return null;
    }

    public static boolean deleteCauHoi(String maCauHoi) {
        Connection con = null;
        PreparedStatement stm = null;

        try {
            con = Utilities.DBUtils.makeConnection();
            String sql = "Delete From dbo.CauHoi where MaCauHoi = ? ";
            stm = con.prepareStatement(sql);
            stm.setInt(1, Integer.parseInt(maCauHoi));
            int row = stm.executeUpdate();
            if (row > 0) {
                return true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {

                if (con != null) {
                    con.close();
                }
                if (stm != null) {
                    stm.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }

        }
        return false;
    }
  

     public static boolean updatCauHoi(String maNhanVien, String noiDungTraLoi, String maCauHoi) {
        Connection con = null;
        PreparedStatement stm = null;

        try {
            con = DBUtils.makeConnection();
            String sql = "Update dbo.CauHoi set MaNhanVien = ? , NoiDungTraLoi = ? where MaCauHoi = ? ";
            stm = con.prepareStatement(sql);
            stm.setInt(1, Integer.parseInt(maNhanVien));
            stm.setString(2, noiDungTraLoi);
            stm.setInt(3, Integer.parseInt(maCauHoi));
            int row = stm.executeUpdate();
            if (row > 0) {
                return true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {

                if (con != null) {
                    con.close();
                }
                if (stm != null) {
                    stm.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }

        }
        return false;

    }
      public static CauHoiDTO[] searchCauHoiById(String id) {
        Connection con = null;
        PreparedStatement stm = null;
        ResultSet rs = null;
        try {
            con = Utilities.DBUtils.makeConnection();
            String sql = "SELECT * FROM dbo.CauHoi WHERE MaCauHoi = ?";
            stm = con.prepareStatement(sql);
            stm.setInt(1, Integer.parseInt(id));
            List list = new ArrayList();
            rs = stm.executeQuery();
            while (rs.next()) {
                int MaCauHoi = rs.getInt("MaCauHoi");
                String CauHoi = rs.getString("CauHoi");
                int CustomerCode = rs.getInt("CustomerCode");
                int MaNhanVien = rs.getInt("MaNhanVien");
                String NoiDungTraLoi = rs.getString("NoiDungTraLoi");
                String NgayGui = rs.getString("NgayGui");
                CauHoiDTO ch = new CauHoiDTO(MaCauHoi, CauHoi, CustomerCode, MaNhanVien, NoiDungTraLoi,NgayGui);
                list.add(ch);
            }
            CauHoiDTO[] result = new CauHoiDTO[list.size()];
            list.toArray(result);
            return result;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (rs != null) {
                    rs.close();
                }
                if (con != null) {
                    con.close();
                }
                if (stm != null) {
                    stm.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }

        }
        return null;
    }
}
